﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LpnBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILpnDA lpnDA = DataAccess.CreateLpnDA();
        private PubParasInfo pubParasInfo;

        public LpnBLL()
        {
            this.pubParasInfo = null;
        }

        public LpnBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public LpnBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteLpn(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<LpnInfo> lpnInfo = new List<LpnInfo>();
                LpnInfo contractInfo = null;
                foreach (string id in IDS)
                {
                    contractInfo = lpnDA.GetLpnByID(dataBase, tran, id, this.dataBaseID[0]);
                    lpnInfo.Add(contractInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (lpnDA.DeleteLpn(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<LpnInfo>(dataBase, tran, lpnInfo[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "LPN", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GeLpnAllInfo()
        {
            return lpnDA.GetAllLpn(this.dataBaseID[0]);
        }

        public LpnInfo GetInfoByID(string id)
        {
            return lpnDA.GetLpnByID(id, this.dataBaseID[0]);
        }

        public DataSet GetLpnByQueryEntity(LpnQueryEntity lpnQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(lpnQuery.LpnID))
            {
                sqlWhere.Add(" L.LPN_ID " + lpnQuery.LpnID);
            }
            if (!string.IsNullOrEmpty(lpnQuery.PackID))
            {
                sqlWhere.Add(" L.PACK_ID" + lpnQuery.PackID);
            }
            if (!string.IsNullOrEmpty(lpnQuery.DocID))
            {
                sqlWhere.Add(" L.DOC_ID " + lpnQuery.DocID);
            }
            if (!string.IsNullOrEmpty(lpnQuery.DocType))
            {
                sqlWhere.Add(" L.DOC_TYPE " + lpnQuery.DocType);
            }
            if (!string.IsNullOrEmpty(lpnQuery.IsActive))
            {
                sqlWhere.Add(" L.IS_ACTIVE " + lpnQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(lpnQuery.OwnerID))
            {
                sqlWhere.Add(" ILLL.OWNER_ID" + lpnQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(lpnQuery.SkuID))
            {
                sqlWhere.Add(" ILLL.SKU_ID " + lpnQuery.SkuID);
            }
            if (lpnQuery.History == "N")
            {
                sqlWhere.Add(" L.QTY > 0  ");
            }
            if (!string.IsNullOrEmpty(lpnQuery.ExternID))
            {
                sqlWhere.Add(" (O.EXTERN_ORDER_ID " + lpnQuery.ExternID + " OR R.EXTERNAL_RECEIPT_ID" + lpnQuery.ExternID + ")");
            }
            return lpnDA.GetLpnByQueryList(sqlWhere, lpnQuery, this.dataBaseID[0]);
        }

        public string GetMaxLpnByLpnID(string lpnID, string length)
        {
            return lpnDA.GetMaxLpnByLpnID(lpnID, length, this.dataBaseID[0]).Trim();
        }

        public string InsertLpn(LpnInfo lpnInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strLpnID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(lpnInfo.LpnID))
                {
                    lpnInfo.LpnID = commonMethod.GenNextNumStr(dataBase, tran, "LPN", whLoginID);
                }
                lpnInfo.UpdatedBy = lpnInfo.CreatedBy;
                lpnInfo.CreatedDate = DateTime.Now;
                lpnInfo.UpdatedDate = DateTime.Now;
                if (lpnDA.InsertLpn(dataBase, tran, lpnInfo, whLoginID) > 0)
                {
                    strLpnID = lpnInfo.LpnID;
                    new AuditDataBLL().InsertAuditData<LpnInfo>(dataBase, tran, null, lpnInfo, AuditAction.Insert, this.pubParasInfo.UserID, "LPN", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strLpnID;
        }

        public string LPNImportByData(string[][] ImportByData)
        {
            Exception ex;
            try
            {
                int i;
                string whLoginID = this.dataBaseID[0];
                string lpnExist = string.Empty;
                List<LpnInfo> lpnInfos = new List<LpnInfo>();
                for (i = 1; i < ImportByData.Length; i++)
                {
                    LpnInfo lpnInfo = new LpnInfo();
                    for (int j = 0; j < ImportByData[i].Length; j++)
                    {
                        string CS40001 = ImportByData[0][j].ToUpper().Trim();
                        if (((CS40001 != null) && (CS40001 == "LPN")) && !string.IsNullOrEmpty(ImportByData[i][j]))
                        {
                            lpnInfo.LpnID = ImportByData[i][j];
                        }
                    }
                    lpnInfo.PackID = "";
                    lpnInfo.Status = "OK";
                    lpnInfo.DocType = "IN";
                    lpnInfo.DocID = "";
                    lpnInfo.Qty = 0M;
                    lpnInfo.CreatedBy = this.pubParasInfo.UserID;
                    lpnInfo.UpdatedBy = this.pubParasInfo.UserID;
                    lpnInfo.IsActive = "Y";
                    if (lpnDA.CheckLpnIDUnique(lpnInfo.LpnID, whLoginID))
                    {
                        string CS00003 = lpnExist;
                        string[] CS00004 = new string[] { CS00003, lpnInfo.LpnID, "(", (i + 1).ToString(), ");" };
                        lpnExist = string.Concat(CS00004);
                    }
                    lpnInfos.Add(lpnInfo);
                }
                if (!string.IsNullOrEmpty(lpnExist))
                {
                    throw new Exception("导入失败：" + lpnExist + "系统已经存在！");
                }
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    for (i = 0; i < lpnInfos.Count; i++)
                    {
                        lpnInfos[i].CreatedDate = DateTime.Now;
                        lpnInfos[i].UpdatedDate = DateTime.Now;
                        if (lpnDA.InsertLpn(dataBase, tran, lpnInfos[i], whLoginID) > 0)
                        {
                            new AuditDataBLL().InsertAuditData<LpnInfo>(dataBase, tran, null, lpnInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "LPN", this.dataBaseID[0]);
                        }
                    }
                    tran.Commit();
                }
                catch (Exception exception1)
                {
                    ex = exception1;
                    tran.Rollback();
                    CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                }
                finally
                {
                    conn.Close();
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return "";
        }

        public string UpdateLpn(LpnInfo lpnInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strLpnID = string.Empty;
            try
            {
                LpnInfo oldlpnInfo = lpnDA.GetLpnByID(dataBase, tran, lpnInfo.LpnID, this.dataBaseID[0]);
                if (lpnDA.UpdateLpn(dataBase, tran, lpnInfo, whLoginID) > 0)
                {
                    strLpnID = lpnInfo.LpnID;
                    new AuditDataBLL().InsertAuditData<LpnInfo>(dataBase, tran, oldlpnInfo, lpnInfo, AuditAction.Update, this.pubParasInfo.UserID, "LPN", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strLpnID;
        }
    }
}

